// pages/communityService/communityService.js
const app = getApp();
var utils = require('../../utils/util');
var json =  require('../../data/json.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
      indicatorDots: true,
      autoplay: true,
      interval: 5000,
      duration: 1000,
      // 滚动数据
      list: [
          // {
          // url: "https://jzj.freei.cc/Uploads/candelete/super-market-ad01.png",
          // id: 1
          // },
          // {
          //     url: "https://jzj.freei.cc/Uploads/candelete/super-market-ad01.png",
          //     id: 2
          // },
          // {
          //     url: "https://jzj.freei.cc/Uploads/candelete/super-market-ad01.png",
          //     id: 3
          // }

      ],
      isBanner: false,
      currentSwiper: 0,
      healthImgList: [],
      iconNavList:[
          {
              // background: '#ff3d4b',
              url:'/resources/communityService/icon_new01.png',
              iconW: 80,
              iconH: 80,
              text: '家政服务',
              index: 1,
              link: '/subPages/communityService/HomemakingService/HomemakingService'
          },
          {
              // background: '#f12764',
              url:'/resources/communityService/icon_new02.png',
              iconW: 80,
              iconH: 80,
              text: '社区商城',
              index: 2,
              link:"/subPages/communityService/communityShop/communityShop"
          },
          {
              // background: '#ff7950',
              url:'/resources/communityService/icon_new03.png',
              iconW: 80,
              iconH: 80,
              text: '健康服务',
              index: 3,
              link:"/subPages/communityService/healthService/healthService"
          },
          {
              // background: '#e87417',
              url:'/resources/communityService/icon_new04.png',
              iconW: 80,
              iconH: 80,
              text: '特膳厨房',
              index: 4,
              link:'/subPages/pensionService/newKitchen/newKitchen?id=1'
          },{
              url:'/resources/communityService/icon_new05.png',
              text:'二手商城',
              iconW: 80,
              iconH: 80,
              index:5,
              link:'/subPages/communityService/secondHandMall/secondHandMall'
          }
      ]

  },
    toPharmacy(){
      wx.navigateTo({
          url:'/subPages/communityService/pharmacy/pharmacy'
      })
    },
    toAll(){
        wx.navigateTo({
            url:'/subPages/communityService/presaleSupermaketAll/presaleSupermaketAll'
        })
    },
    //获取顶部banner图列表
    getBanner(){
      var that = this;
      utils.getAjax(`${app.globalData.apiUrl}/get_community_service_banner_yes`,function(res){
        //   console.log(res);
          that.setData({
              healthImgList:res.data,
              isBanner: true
          })
      },{community_id: app.globalData.communityId})
    },
    //BANNER 滑动
    swiperChange:function(e){
        this.setData({
            currentSwiper: e.detail.current
        })
    },
    imgNavTo(e){
      var id = e.currentTarget.dataset.id
        // console.log(id);
      wx.navigateTo({
          url:'/subPages/communityService/PresaleSupermarket/PresaleSupermarket?id='+id
      })
    },
    //图标导航路由
    iconNavBtn(e){
        var that = this
        let index = e.currentTarget.dataset.index;
        wx.navigateTo({
            url:this.data.iconNavList[index].link
        })
    },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    utils.setNavBarColor('#ffffff',app.globalData.themeColor);
    if(app.globalData.communityId){
        that.setData({
            communityId: app.globalData.communityId
        });
        that.getBanner();
        //超市促销banner limit 1 显示3项  2 显示全部
        utils.getAjax(app.globalData.apiUrl+'/get_community_service_banner',that.setSupermarketSale,{community_id: that.data.communityId,limit:1})
    }else{
        app.communityIdCallback = communityId => {
            if (communityId != '' && communityId != null) {
                that.setData({
                    communityId: communityId
                });
                that.getBanner();
                //超市促销banner limit 1 显示3项  2 显示全部
                utils.getAjax(app.globalData.apiUrl+'/get_community_service_banner',that.setSupermarketSale,{community_id: that.data.communityId,limit:1})
            }
        }
    }
  },
    setSupermarketSale(res){
      var that = this;
      // console.log(res);
      if(res.data){
        that.setData({
            isMarket: true,
            list:res.data
        })
      }else{
        that.setData({
            isMarket: false
        })
      }
    },


    imgBtn:function(e){
        var list = e.currentTarget.dataset.list
        var index = e.currentTarget.dataset.index
        if(list[index].url_id){
            switch (list[index].url_id) {
            case '1':
                wx.navigateTo({
                    url: '/subPages/communityService/HomemakingService/HomemakingService',
                })
                break;
            case '2':
                wx.navigateTo({
                    url: '/subPages/communityService/communityShop/communityShop',
                })
                break;
            case '3':
                wx.navigateTo({
                    url: '/subPages/communityService/healthService/healthService',
                })
                break;
            case '4':
                wx.navigateTo({
                    url: '/subPages/pensionService/newKitchen/newKitchen?id=1',
                })
                break;
            case '5':
                wx.navigateTo({
                    url: '/subPages/communityService/secondHandMall/secondHandMall',
                })

            }
        }
    },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    var that = this
    if(that.data.communityId){
        that.getBanner();
        //超市促销banner limit 1 显示3项  2 显示全部
        utils.getAjax(app.globalData.apiUrl+'/get_community_service_banner',that.setSupermarketSale,{community_id: that.data.communityId,limit:1})
    }
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})
